Skip to main content

Overview

Fine-tuning allows you to adapt pretrained CLIP models to specific domains or datasets by continuing training from a pretrained checkpoint. This is often more efficient than training from scratch and can achieve better performance with less data.

When to Fine-tune

✅ Fine-tune when:

  • You have a pretrained model that’s close to your target domain
  • You have limited training data (1M-100M samples)
  • You want to adapt to a specific domain (medical images, satellite imagery, etc.)
  • You need faster convergence than training from scratch
  • You want to improve zero-shot performance on specific tasks

❌ Train from scratch when:

  • Your domain is very different from the pretrained model’s training data
  • You have massive amounts of training data (>1B samples)
  • You need a completely custom architecture
  • You want to experiment with new training objectives

Loading Pretrained Weights

From OpenCLIP Pretrained Models

Use the --pretrained flag with a model tag:
Available pretrained tags:
Common pretrained weights:
  • laion2b_s34b_b79k: ViT-B/32 on LAION-2B
  • laion2b_s32b_b82k: ViT-L/14 on LAION-2B
  • openai: Original OpenAI CLIP weights
  • datacomp_xl_s13b_b90k: DataComp-1B models

From Local Checkpoint

Use a local checkpoint file:

From Hugging Face Hub

Download from Hugging Face and use local path:

Resuming Training from Checkpoint

The --resume flag continues training from a checkpoint, including optimizer state:
Resume vs Pretrained:

Resume from Latest Checkpoint

Automatically finds and loads the most recent checkpoint in the logs directory.

Fine-tuning Strategies

1. Full Model Fine-tuning

Fine-tune all parameters with a lower learning rate:
Key changes from pretraining:
  • ⬇️ Lower learning rate: 1e-5 vs 1e-3 for pretraining
  • ⏱️ Fewer epochs: 10 vs 32 for pretraining
  • 🔥 Shorter warmup: 1000 vs 10000 steps

2. Frozen Image Encoder (Text-Only Fine-tuning)

Freeze the image encoder and only fine-tune the text encoder:
Benefits:
  • 💾 Lower memory usage
  • ⚡ Faster training
  • 🎯 Useful when adapting to new vocabulary/concepts

3. Frozen Text Encoder (Image-Only Fine-tuning)

Freeze the text encoder and only fine-tune the image encoder:
Use cases:
  • Adapting to new image domains (medical, satellite, etc.)
  • Maintaining text understanding while improving visual features

4. Partial Fine-tuning

Freeze early layers and fine-tune later layers:
Benefits:
  • ⚖️ Balance between adaptation and preservation
  • 💾 Lower memory and compute requirements
  • 🛡️ Less prone to overfitting on small datasets

5. LiT (Locked Image Tuning)

Lock image encoder with ImageNet pretrained weights, train text encoder from scratch:
Reference: LiT: Zero-Shot Transfer with Locked-image Text Tuning

Learning Rate Adjustment

Fine-tuning requires careful learning rate selection:

Learning Rate Schedules

Cosine with warmup (recommended):
Constant with warmup:
Constant with cooldown:

Fine-tuning Examples

Domain Adaptation: Medical Images

Small Dataset Fine-tuning

Multilingual Fine-tuning

High-Resolution Fine-tuning

WiSE-FT: Robust Fine-tuning

For robust fine-tuning that maintains performance under distribution shift, use the WiSE-FT repository. WiSE-FT (Weight-Space Ensembling for Fine-Tuning) averages the weights of:
  1. Zero-shot pretrained model
  2. Fine-tuned model
This preserves robustness while improving accuracy.

WiSE-FT Workflow

Reference: Robust Fine-tuning of Zero-shot Models

Monitoring Fine-tuning

Zero-shot Evaluation

Track zero-shot performance during fine-tuning:
Monitor both:
  • Fine-tuning dataset performance (improves)
  • Zero-shot ImageNet accuracy (may degrade if overfitting)

Validation Loss

Weights & Biases Logging

Common Fine-tuning Issues

Overfitting

Symptoms:
  • Training loss decreases, validation loss increases
  • Zero-shot performance degrades significantly
Solutions:
  1. Reduce learning rate
  2. Use fewer epochs
  3. Freeze more layers
  4. Add regularization (increase --wd)
  5. Use more data augmentation

Underfitting

Symptoms:
  • Both training and validation loss remain high
  • No improvement over pretrained model
Solutions:
  1. Increase learning rate
  2. Train for more epochs
  3. Unfreeze more layers
  4. Reduce regularization

Catastrophic Forgetting

Symptoms:
  • Good performance on fine-tuning dataset
  • Poor zero-shot performance on general tasks
Solutions:
  1. Use lower learning rate
  2. Freeze early layers
  3. Use WiSE-FT weight ensembling
  4. Mix fine-tuning data with general data

Best Practices

Fine-tuning checklist:
  1. ✅ Start with a pretrained model close to your domain
  2. ✅ Use 10-100× lower learning rate than pretraining
  3. ✅ Fine-tune for 5-20 epochs (much less than pretraining)
  4. ✅ Monitor both task performance and zero-shot performance
  5. ✅ Try partial fine-tuning before full fine-tuning
  6. ✅ Use validation set to prevent overfitting
  7. ✅ Consider WiSE-FT for robust fine-tuning
  8. ✅ Save checkpoints frequently for comparison
Avoid:
  • ❌ Using same learning rate as pretraining
  • ❌ Fine-tuning for too many epochs
  • ❌ Ignoring zero-shot performance degradation
  • ❌ Not using validation data
  • ❌ Forgetting to set --pretrained flag

Fine-tuning Templates

Quick Fine-tuning (Small Dataset)

Production Fine-tuning (Large Dataset)

Conservative Fine-tuning (Preserve Generalization)

Next Steps

Training Overview

Learn about training CLIP models from scratch

Configuration

Explore all fine-tuning parameters

Pretrained Models

Browse available pretrained models

WiSE-FT

Learn about robust fine-tuning with weight ensembling